package com.sport.setting.repository;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sport.model.setting.dtos.SportEventsGamesChooseDto;
import com.sport.model.setting.pojos.SportEventsPreEnroll;
import com.sport.setting.mapper.SportEventsGameGradeMapper;
import com.sport.utils.common.ThreadPoolUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SportEventsGameGradeRepository extends ServiceImpl<SportEventsGameGradeMapper, SportEventsPreEnroll> {

    @Resource
    private SportEventsGameGradeMapper sportEventsGameGradeMapper;

    /**
     * 思考是将查询并插入工作交给数据库还是资源库
     * @param sportEventsGamesChooseDtoList
     * @return
     */
    public Integer setEventsGameForGrade(List<SportEventsGamesChooseDto> sportEventsGamesChooseDtoList) {
        if (sportEventsGamesChooseDtoList == null)
            return 0;

//        List<SportEventsPreEnroll> sportEventsPreEnrolls = new ArrayList<>();
//        for (int single = 0; single < sportEventsGamesChooseDtoList.size(); ++single) {
//            SportEventsGamesChooseDto sportEventsGamesChooseDto = sportEventsGamesChooseDtoList.get(single);
//
//            SportEventsPreEnroll target = new SportEventsPreEnroll(
//                    sportEventsGamesChooseDto.getChooseSportGrade()
//                    , sportEventsGamesChooseDto.getChooseSportEventsGames().getId()
//                    , sportEventsGamesChooseDto.getSportEventsMale()
//                    , sportEventsGamesChooseDto.getSportEventsFemale()
//                    , 0
//                    , sportEventsGamesChooseDto.getSportEventsTime()
//                    , sportEventsGamesChooseDto.getSportEventsMember()
//                    , sportEventsGamesChooseDto.getAccountUserBo().getSportId()
//                    , sportEventsGamesChooseDto.getAccountUserBo().getSchoolTenementId()
//            );
//
//            if (sportEventsGameGradeMapper.selectList(new QueryWrapper<>(target)) != null)
//                continue;
//
//            sportEventsPreEnrolls.add(target);
//        }
//
//        saveBatch(sportEventsPreEnrolls);

        for (int single = 0; single < sportEventsGamesChooseDtoList.size(); ++single) {
            SportEventsGamesChooseDto sportEventsGamesChooseDto = sportEventsGamesChooseDtoList.get(single);

            SportEventsPreEnroll target = new SportEventsPreEnroll(
                    sportEventsGamesChooseDto.getChooseSportGrade()
                    , sportEventsGamesChooseDto.getChooseSportEventsGames().getId()
                    , sportEventsGamesChooseDto.getSportEventsMale()
                    , sportEventsGamesChooseDto.getSportEventsFemale()
                    , 0
                    , sportEventsGamesChooseDto.getSportEventsTime()
                    , sportEventsGamesChooseDto.getSportEventsMember()
                    , sportEventsGamesChooseDto.getAccountUserBo().getSportId()
                    , sportEventsGamesChooseDto.getAccountUserBo().getSchoolTenementId()
            );

            if (sportEventsGameGradeMapper.selectInfo(target) == 1) continue;

            sportEventsGameGradeMapper.insertAfterSelect(target);
        }

        return 1;
    }
}
